﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace gprs_config_tools
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "二进制文件(*.bin)|*.bin";
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string path = openFileDialog1.FileName;
                textBox1.Text = path;

                //使用“打开”对话框中选择的文件名实例化FileStream对象
                FileStream myStream = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                //使用FileStream对象实例化BinaryReader二进制读取流对象
                BinaryReader myReader = new BinaryReader(myStream);
                if (myReader.PeekChar() != -1)
                {
                    //以二进制方式读取文件中的内容
                    byte[] bBuffer = new byte[myStream.Length];
                    myReader.Read(bBuffer, 0, (int)myStream.Length);
;
                    int i = 0;
                    StringBuilder ret = new StringBuilder();
                    foreach (byte b in bBuffer)
                    {
                        ret.AppendFormat("{0:x2} ", b);//{0:X2} 大写
                        if((++i % 64) == 0)
                        {
                            ret.AppendFormat("\r\n\r\n");
                        }
                    }

                    richTextBox1.Text = ret.ToString();
                }
                //关闭当前二进制读取流
                myReader.Close();
                //关闭当前文件流
                myStream.Close();

            }
         }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }
    }
}
